local function onskillpoint(self)
    self.inst.replica.i26skill:SetPoint(self.skillpoint)
end
local function onsuperatk(self, val)
    if val then
        self.inst:AddTag("i26sppatk")
    else
        self.inst:RemoveTag("i26sppatk")
    end
end

local i26skill = Class(function(self, inst) 
    self.inst = inst
    
    self.dd_p = .8  --格挡强度
    self.dd_c = 0   --格挡概率
    self.ddc = .2   -- 基础概率
    --self.DZ_TA = nil  --大招回转
    --self.DT_DDC = nil --动态格挡概率
    --i26skill.DD_TOPER --持续格挡

    self.temp_dd_c = 0
    self.superatk = false
    self.dzatk = false
    self.poweratk = false
    self.atkmult = 1
    self.aoeatk = false

    self.rcost = 2

    self.skillpointmax = TUNING.LINKAGE.SPARKLE and 7 or 5
    self.skillpoint = 0

    self.bdmult = 1

    self.bdcd = 120
    --self.inst:StartUpdatingComponent(self)
    self.dpttask1 = self.inst:DoPeriodicTask(1, function() --比Update节省性能
        if self.bdtask ~= nil then return end 
        local health = self.inst.components.health
        if health:IsDead() or self.inst.components.hunger.current == 0 then
            return 
        end
        if health.currenthealth < health.maxhealth then
            self:EnableBD()
        end
    end)
    self.dpttask2 = self.inst:DoPeriodicTask(30, function()
        self:DoDelta(1)
    end)

    inst:ListenForEvent("equip", function()
        self:WeaponCn()
    end)
    inst:ListenForEvent("unequip", function()
        self:WeaponCn()
    end)
end,
nil,
{   --变化时自动运行
    skillpoint = onskillpoint,
    superatk = onsuperatk,
})

function i26skill:OnSave()
    local data = {
        skillpoint = self.skillpoint,
        ohealth = self.inst.components.health and self.inst.components.health.currenthealth
    }
    return data
end
function i26skill:OnLoad(data)
    if not data then return end
    self.skillpoint = data.skillpoint or 0

    if data.ohealth then
        self.inst:DoTaskInTime(1/10, function()
            local health = self.inst.components.health
            if health and not health:IsDead() then
                health.currenthealth = data.ohealth
                health:DoDelta(0, true)
            end
        end)
        self.inst:DoTaskInTime(2, function()
            local health = self.inst.components.health
            if health and not health:IsDead() then
                health:DoDelta(0, true)
            end
        end)
    end
    
end

function i26skill:WeaponCn()
    local weapon = self.inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon then
        self.dd_c = self.ddc
    else
        self.dd_c = 0
        self:CancelSuperAtk(weapon)
    end
end

function i26skill:CanDD()
    local ddc = self.dd_c + self.temp_dd_c
    if self.DT_DDC then
        ddc = ddc * math.min(2 - (self.inst.components.health:GetPercent() * 2), 1)
    end
    return math.random() <= ddc
end

function i26skill:IsFull()
    return self.skillpoint >= self.skillpointmax
end

function i26skill:DoDelta(delta, overtime)
    if delta > 0 and self:InHHXBF() then
        delta = delta * 2
    end
    local newval = math.clamp(self.skillpoint + delta, 0, self.skillpointmax)
    self.skillpoint = newval
    self.inst:PushEvent("i26spdelta",{ point = newval })
end


function i26skill:CanSuperAtk()
    return self.superatk
end
function i26skill:GetMult()
    return self.atkmult
end
function i26skill:IsAOE()
    return self.aoeatk
end


function i26skill:SpawnFolFx(isice)
    self:KillFolFx()
    self.lightfx = SpawnPrefab(isice and "deer_ice_charge" or "deer_fire_charge")
    self.lightfx.entity:SetParent(self.inst.entity)
    self.lightfx.entity:AddFollower()
    self.lightfx.Follower:FollowSymbol(self.inst.GUID, "swap_object", 0, 0, 0)

    -- local weapon = self.inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    -- if weapon and weapon:HasTag("i26_laotie") then
    --     self.lt_blfx = SpawnPrefab("i26faguangfx")
    --     self.lt_blfx.entity:SetParent(self.inst.entity)
    --     self.lt_blfx.entity:AddFollower()
    --     self.lt_blfx.Follower:FollowSymbol(self.inst.GUID, "swap_object", 0, 0, 0)
    -- end

    return self.lightfx
end
function i26skill:KillFolFx()
    if self.lightfx ~= nil and self.lightfx:IsValid() then
        self.lightfx:Remove()   --
        self.lightfx = nil
    end
    -- if self.lt_blfx ~= nil and self.lt_blfx:IsValid() then
    --     self.lt_blfx:Remove()   --
    --     self.lt_blfx = nil
    -- end
end

function i26skill:EcSuperAtk()
    if self.superatk then return end 
    local inst = self.inst
    self.temp_dd_c = 0
    self.superatk = true
    self.aoeatk = true

    self:SpawnFolFx()
    
    local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon:HasTag("i26_laotie") then
        weapon.components.i26egotool:EnableLunacy(true, "spatk")
    end
    
    if self.cancelsa_task ~= nil then
        self.cancelsa_task:Cancel()
        self.cancelsa_task = nil 
    end
    self.cancelsa_task = self.inst:DoTaskInTime(5, function()
        self:CancelSuperAtk(weapon)
    end)
end
function i26skill:CancelSuperAtk(weapon)
    if not self.superatk then return end 
    if self.cancelsa_task ~= nil then
        self.cancelsa_task:Cancel()
        self.cancelsa_task = nil 
    end
    
    self.superatk = false
    self.aoeatk = false
    if self.dzatk then
        --self.inst:PushEvent("i26s3hit")
    end
    self.dzatk = false
    self:KillFolFx()
    self.atkmult = 1
    if weapon and weapon.components.i26egotool then
        weapon.components.i26egotool:EnableLunacy(false, "spatk")
    end
end


--------------

local function Skill1(inst, x, y, z)
    local weapon = inst.components.inventory and inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon:HasTag("i26_laotie") then  
        local fx = SpawnPrefab("farm_plant_happy")
        fx.entity:SetParent(inst.entity)
        fx.Transform:SetPosition(0,0,0)
        inst.components.health:DoDelta(TUNING.i26_SKILLR_HEAL)
        inst.sg:GoToState("i26sgskill1", {pos = Vector3(x,y,z)})

    else
        inst.components.health:DoDelta(inst.components.i26skill and inst.components.i26skill.TOPTER and 30 or 10)
        local xx, yy, zz = inst.Transform:GetWorldPosition()
        local laotie 
        if inst.droplaotie and inst.droplaotie:IsValid() then
            laotie = inst.droplaotie
        else
            for k,v in pairs(TheSim:FindEntities(xx, yy, zz, 40, {"i26_laotie"})) do
                if v:IsValid() then
                    local owner = v.components.inventoryitem:GetGrandOwner()
                    if owner == nil or not owner:HasTag("player") or owner == inst then
                        laotie = v
                        break
                    end
                end
            end
        end

        if laotie then
            local tzhz = inst.components.i26skill and inst.components.i26skill.TOPTER 
            and laotie.components.i26egotool and laotie.components.i26egotool:GetThrowHZ()
            if tzhz and inst.components.i26skill then
                inst.components.i26skill:DoDelta(1)
            end
            local x2, y2, z2 = laotie.Transform:GetWorldPosition()
            inst.Transform:SetPosition(x2, 0, z2)
            inst.components.inventory:Equip(laotie)
        else
            -- local r = 32
            -- if distsq(xx, zz, x, z) > r * r then
            --     local theta = inst.Transform:GetRotation() * DEGREES
            --     x = xx + r * math.cos(theta)
            --     z = zz - r * math.sin(theta)
            -- end
            inst.components.i26skill:DoDelta(1)
            inst.Transform:SetPosition(xx, yy, zz)
        end
        
        --inst.droplaotie = nil
    end
end

local function ChaoFeng(inst)
    local x, y, z = inst.Transform:GetWorldPosition()
    local notags = {"player", "INLIMBO", "wall", "companion", "FX", "weapon"}
    for k,v in pairs(TheSim:FindEntities(x, y, z, 16, {"_combat"}, notags)) do
        if v:IsValid() 
        and v.components.combat and v.components.combat.target ~= nil 
        and v.components.health and not v.components.health:IsDead()
        and (v.components.follower == nil or v.components.follower.leader == nil or not v.components.follower.leader:HasTag("player"))
        then
            v.components.combat:SetTarget(inst)
        end
    end
end

local function Skill2(inst, x, y, z)
    --inst:PushEvent("i26s3start")
    inst.i26s3start:push()
    inst.sg:GoToState("i26sgskill2")
    ChaoFeng(inst)
end



function i26skill:Cost(cost)
    cost = cost or 1 
    if self.skillpoint >= cost then
        self:DoDelta(-cost)
        return true
    end
    if self.inst.components.talker then
        self.inst.components.talker:Say("战技点不够")
    end
    return false
end
function i26skill:DoSkill(id, x, y, z)  -- 中转
    self.inst:ForceFacePoint(x, y, z)
    if id == 1 and self:Cost() then
        self.inst.components.i26skill:EnBt(1)
        return Skill1(self.inst, x, y, z)
    elseif id == 2 and self:Cost(self.rcost) then
        self.inst.components.i26skill:EnBt(1)
        return Skill2(self.inst, x, y, z)
    end
    return true
end

function i26skill:DoSuper1()
    local inst = self.inst
    self:KillFolFx()
    self.temp_dd_c = 0
    self.superatk = true
    self.aoeatk = true
    self.atkmult = 2.2
    self.dzatk = true

    self:SpawnFolFx(true)
    
    local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon:HasTag("i26_laotie") then
        weapon.components.i26egotool:EnableLunacy(true, "spatk")
    end
end
function i26skill:DoSuper2()
    local inst = self.inst
    self:KillFolFx()
    self.temp_dd_c = 0
    self.superatk = true
    self.aoeatk = false
    self.dzatk = true
    self.atkmult = 1.5

    self:SpawnFolFx(true).AnimState:SetScale(.5, .5)

    local weapon = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
    if weapon and weapon:HasTag("i26_laotie") then
        weapon.components.i26egotool:EnableLunacy(true, "spatk")
    end
end

function i26skill:GetAtk(attacker, damage, weapon, stimuli, spdamage)
    if self.superdef then   --大招
        --self.inst:PushEvent("i26attacked")
        if self.DZ_TA and math.random() <= self.dd_c / 2 then --回转
            self:DoDelta(1)
        end
        self.inst.i26attacked:push()
        self:DoSuper1()
        if self.dfx2 == nil or not self.dfx2:IsValid() then
            self.dfx2 = SpawnPrefab("i26ddfx2"):ATC(self.inst)
        end
        self.inst.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour")
        return true
    end
    local candd = self:CanDD()
    
    -- if self.dd_c > 0 then
    --     candd = self:CanDD()
    -- else 
    --     return false, damage, spdamage
    -- end
    
    if candd then 
        self.inst.i26dd:push()
        --self.inst:PushEvent("i26dd")
        SpawnPrefab("i26ddfx1"):ATC(self.inst)
        self:EcSuperAtk()
        damage = damage * self.dd_p

        local spds = {}
        if spdamage ~= nil and next(spdamage) then
            for k,v in pairs(spdamage) do
                spds[k] = v * self.dd_p
            end
        else
            spds = nil
        end
        
        return false, damage, spds
    end
    self.temp_dd_c = self.temp_dd_c + .2
    return false, damage, spdamage
end

function i26skill:EnBt(time)
    self:UnBt()
    if time ~= nil then
        self.cbttask = self.inst:DoTaskInTime(time, function()
            self:UnBt()
        end)
    end
    self.inst:AddTag("i26bati")
    self.inst.components.inventory.isexternallyinsulated:SetModifier(self.inst, true)
end
function i26skill:UnBt()
    if self.cbttask ~= nil then
        self.cbttask:Cancel()
        self.cbttask = nil
    end
    self.inst:RemoveTag("i26bati")
    self.inst.components.inventory.isexternallyinsulated:RemoveModifier(self.inst)
end

function i26skill:UnEnableBD()
    if self.bdtask ~= nil then
        self.bdtask:Cancel()
        self.bdtask = nil
        self.inst.components.hunger.burnratemodifiers:RemoveModifier(self.inst)
    end
end
function i26skill:EnableBD()
    self:UnEnableBD()
    self.inst.components.hunger.burnratemodifiers:SetModifier(self.inst, 1.5)
    self.bdtask = self.inst:DoPeriodicTask(1, function(inst)
        local health = inst.components.health
        if health:IsDead() or self.inst.components.hunger.current == 0 then 
            self:UnEnableBD()
        return end 
        if self.TOPTER and inst.i26onbattle_task ~= nil then return end --战斗状态
        
        health:DoDelta(1.25 * self.bdmult, true, "file_load", true, nil, true)
        if health:GetPenaltyPercent() > 0 and health.currenthealth >= health:GetMaxWithPenalty() then
            health:DeltaPenalty(-.01)
        end
        
        if health.currenthealth >= health.maxhealth then
            self:UnEnableBD()
        end
    end)
end

-- 技能树 伤害
    -- function i26skill:rb_hp_cb()
    --     local inst = self.inst 
    --     if not inst or not inst:IsValid() then return end 
    --     local hpmax = inst.components.health.maxhealth 
    --     local ychps = hpmax - 50 - TUNING.YUNLI_HEALTH
    --     if ychps > 0 then
    --         self.zancun_hps = ychps
    --         inst.components.health:SetMaxHealth(TUNING.YUNLI_HEALTH + 50)
    --         inst.components.combat.externaldamagemultipliers:SetModifier("i26_skill_d1", 1 + (ychps/100))
    --     end
    -- end
    -- function i26skill:REBUILD_HPS()
    --     self.skill_d1 = true
    --     self.inst:DoTaskInTime(0, function()
    --         self:rb_hp_cb()
    --     end)
    -- end
    -- function i26skill:CANCEL_REBUILD_HPS()
    --     self.skill_d1 = false
    --     local combat = self.inst.components.combat
    --     if combat then
    --         combat.externaldamagemultipliers:RemoveModifier("i26_skill_d1")
    --     end
    --     if self.zancun_hps ~= nil then
    --         local hpmax = self.inst.components.health and self.inst.components.health.maxhealth 
    --         if hpmax then
    --             self.inst.components.health:SetMaxHealth(TUNING.YUNLI_HEALTH + 50 + hpmax)
    --         end
    --     end
    -- end
--
function i26skill:InHHXBF() --藿藿大招buff期间
    return self.inst.components.huohuo_buff and self.inst.components.huohuo_buff:HasBuff("huohuo_buff2")
end

i26skill.LongUpdate = function(self, dt)

end

return i26skill